home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 0022-3.564 / dmg-3442 / st_applc / pf_sta12 / listing1.asc next >
Text File  |  1992-04-03  |  8KB  |  190 lines

  1. **
  2. ** Listing 1.
  3. ** Programmers' Forum, November 1991
  4. **
  5. ** Fragment of code showing a method of
  6. ** forcing a reboot on a colour resolution
  7. ** change.  This should be installed in an
  8. ** accessory.
  9. **
  10. ** Assembler: asm v5.06
  11. ** Written: 11th September 1991
  12. **
  13.  
  14. **
  15. ** Define the symbols we use...
  16. **
  17.  
  18. MAX_SIZE  equ       $1060               Assumed maximum DESKTOP.INF size.
  19.  
  20. reset_vec equ       $0004               System variables.
  21. trap1     equ       $0084
  22. _bootdev  equ       $0446
  23.  
  24. GEMDOS    equ       1                   Operating system services.
  25. Fopen     equ       $3D
  26. Fclose    equ       $3E
  27. Fread     equ       $3F
  28. Fwrite    equ       $40
  29. Fseek     equ       $42
  30. Malloc    equ       $48
  31. Mfree     equ       $49
  32.  
  33. XBIOS     equ       14
  34. Getrez    equ       4
  35.  
  36.  
  37. ;=======================================================================
  38. **
  39. ** Installation code for the resolution-change
  40. ** code.  In most cases you would probably want
  41. ** to amalgamate this into your accessory's
  42. ** installation code.  Note that, as written,
  43. ** this routine MUST be called in supervisor mode.
  44. ** _pbase is the Lattice C 5 variable that holds the
  45. ** address of the program's basepage.  If you are using
  46. ** another system, you should ensure that the internal
  47. ** variable 'block' is set up as a pointer to the basepage.
  48. **
  49.  
  50. install   move.l    __pbase,block       Copy our basepage address.
  51.           move.l    trap1,wrecker-4     Grab the GEMDOS trap.
  52.           move.l    #wrecker,trap1      Install us.
  53.           rts
  54.  
  55. ;=======================================================================
  56. **
  57. ** This is the protection against nasty resolution changes.
  58. ** The initialisation code should splice this into the
  59. ** GEMDOS trap.  Before splicing in, the internal variable
  60. ** 'block' should be set to the start of the program's memory
  61. ** block.  Note the XBRA prefix for the handler.
  62. **
  63.  
  64.           dc.l      'XBRA'
  65.           dc.l      'RZCH'              Our XBRA ID: ReZ CHange.
  66.           dc.l      0                   Old GEMDOS trap vector.
  67.  
  68. wrecker   tst.w     recursive           Recursive trap call ?
  69.           beq.s     do_check            No, we need to check malloc.
  70. go_old    move.l    wrecker-4,a0        Don't parse recursive calls,
  71.           jmp       (a0)                Hand off immediately to system.
  72.  
  73. do_check  btst      #13,(sp)            Were we in supervisor mode ?
  74.           beq.s     1$                  No, args are on user stack.
  75.           lea       6(sp),a0            Point a0 to args on supervisor stack.
  76.           bra.s     2$                  Skip on, all set up.
  77. 1$        move.l    USP,a0              Fetch the user stack pointer.
  78. 2$        cmp.w     #Mfree,(a0)         GEMDOS Mfree call ?
  79.           bne.s     go_old              No, pass on to system now.
  80.           move.l    2(a0),a0            Fetch the address to be freed.
  81.           cmp.l     block(pc),a0        Freeing our basepage ?
  82.           bne.s     go_old              No, give it back to the system.
  83. ;
  84. ; We have detected the unloading of this accessory.
  85. ; Now we have to fix the DESKTOP.INF file and reboot.
  86. ; Start by opening the file, allocating a buffer and
  87. ; reading the file into it.
  88. ;
  89.           st        recursive           Pass on all of our GEMDOS calls.
  90.           move.w    _bootdev,d0         Make up the boot drive letter.
  91.           add.w     #'A',d0
  92.           move.b    d0,deskfile         Fix up the DESKTOP.INF filename.
  93. code      pea       MAX_SIZE            Maximum length for DESKTOP.INF.
  94.           move.w    #Malloc,-(sp)       Get us a memory block for the file.
  95.           trap      #GEMDOS
  96.           addq.l    #6,sp
  97.           tst.l     d0                  Memory allocated OK ?
  98.           beq       reboot              No, bomb out.
  99.           move.l    d0,a5               Save the buffer address.
  100.           move.w    #1,-(sp)            We will need R/W access to the file.
  101.           pea       deskfile(pc)
  102.           move.w    #Fopen,-(sp)
  103.           trap      #GEMDOS             Open the file.
  104.           addq.l    #8,sp
  105.           tst.l     d0                  Any error ?
  106.           bmi       reboot              Yes, ice the system.
  107.           move.w    d0,d7               Save the file handle.
  108.           move.l    a5,-(sp)            Space to read the file into.
  109.           pea       MAX_SIZE            Read at most about 4K of file.
  110.           move.w    d0,-(sp)            File handle.
  111.           move.w    #Fread,-(sp)        Read the file in now.
  112.           trap      #GEMDOS
  113.           lea       12(sp),sp
  114.           tst.l     d0                  Did any error occur ?
  115.           bmi.s     reboot              Bomb it if so.
  116.           move.l    d0,d6               Save the number of bytes read.
  117. ;
  118. ; Now reposition the file pointer so our amended
  119. ; block is written back to the correct part of the file.
  120. ;
  121.           clr.w     -(sp)               Seek from start of file.
  122.           move.w    d7,-(sp)            File handle.
  123.           clr.l     -(sp)               Right to the very start of the file.
  124.           move.w    #Fseek,-(sp)
  125.           trap      #GEMDOS             Do it.
  126.           lea       10(sp),sp
  127.           tst.l     d0                  Bomb out on any error.
  128.           bmi.s     reboot
  129. ;
  130. ; At this stage we simply scan the buffer for the entry
  131. ; that determines the resolution. This is a line of the
  132. ; format      #E XX X?
  133. ; where ? is '1' for low res, '2' for medium and '3' for
  134. ; high resolution.
  135. ;
  136.           move.l    a5,a0               Address start of file.
  137.           move.w    d6,d1               Number of bytes in buffer.
  138.           subq.w    #1,d1               Compensate for dbra.
  139.           moveq     #'#',d0             Element prefix character. 
  140. 1$        cmp.b     (a0)+,d0            Scan for it.
  141.           beq.s     3$                  Got one, so process it.
  142. 2$        dbra      d1,1$               Loop until buffer exhausted.
  143.           bra.s     reboot              Resolution not found: error.
  144. 3$        cmp.b     #'E',(a0)           Miscellaneous config information ?
  145.           bne.s     2$                  No, must be another command so skip.
  146. ;
  147. ; We have found the resolution configuration byte.
  148. ; All we do is to find the current resolution, and
  149. ; set the byte to the code for the other colour
  150. ; resolution.
  151. ;
  152.           move.l    a0,a6               Copy the buffer pointer.
  153.           move.b    #'1',6(a6)          Assume new resolution is low.
  154.           move.w    #Getrez,-(sp)       Find out the current resolution.
  155.           trap      #XBIOS
  156.           addq.l    #2,sp
  157.           cmp.w     #1,d0               Currently in medium resolution ?
  158.           beq.s     4$                  Yes, our guess was right.
  159.           move.b    #'2',6(a6)          Wrong, our new resolution is medium.
  160. ;
  161. ; Now we just have to write out the updated file.
  162. ;
  163. 4$        move.l    a5,-(sp)            Write out from the buffer.
  164.           move.l    d6,-(sp)            Holds the real length of the file.
  165.           move.w    d7,-(sp)            File handle.
  166.           move.w    #Fwrite,-(sp)
  167.           trap      #GEMDOS             Write the modified file out now.
  168.           lea       12(sp),sp
  169.           move.w    d7,-(sp)            Finish up the file handling,
  170.           move.w    #Fclose,-(sp)       ensuring that the file is written to
  171.           trap      #GEMDOS             the disk.
  172.           addq.l    #4,sp
  173. ;
  174. ; Now it is safe to reset the system.  This is
  175. ; also done immediately if there has been any
  176. ; error while the routine has been running.
  177. ;
  178. reboot    move.l    reset_vec.w,a0       Jump to do a warm reset.
  179.           jmp       (a0)
  180.  
  181. **
  182. ** Routine data area.
  183. **
  184.  
  185. deskfile  dc.b      'A:\DESKTOP.INF',0
  186. block     dc.l      0
  187. recursive dc.w      0
  188.  
  189.           END
  190.